package de.acando.view;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;

public class MongoDB {
	
	Mongo mongo;
	DB db;
	DBCollection collection;
	
	BasicDBObject document = new BasicDBObject();
	DBCursor cursor;

	public MongoDB(String vorname, String nachname, String email, String passwort) {
		DBConn();
		newUser(vorname, nachname, email, passwort);
	}
	
	public MongoDB(String email, String passwort) {
		DBConn();
		logIn(email, passwort);
	}
	
	private void DBConn(){
		
		try {
			mongo = new Mongo("localhost", 27017);
			db = mongo.getDB("TestDB");
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		
	}
	
	private void logIn(String email, String passwort){
		System.out.println(email+" "+passwort);
		BasicDBObject andQuery = new BasicDBObject();
		List<BasicDBObject> obj = new ArrayList<BasicDBObject>();
		obj.add(new BasicDBObject("email", email));
		obj.add(new BasicDBObject("passwort", passwort));
		andQuery.put("$and", obj);
		collection = db.getCollection("user");
		cursor = collection.find(andQuery);
		while (cursor.hasNext()){
			System.out.println(cursor.next());
		}
	}
	
	private void newUser(String vorname, String nachname, String email, String passwort){
		document = new BasicDBObject();
		collection = db.getCollection("user");
		document.put("vorname", vorname);
		document.put("nachname", nachname);
		document.put("email", email);
		document.put("passwort", passwort);
		
		collection.insert(document);
	}

}
